import requrestUrl from '../../utils/requestUtil.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBut:1,
    userInfo:null,
    openId:null,
    code:null,
    cloudID:null,
    encryptedData:null,
    iv:null
  },
  bindGetPhoneNumber(e){
    this.setData({
      cloudID:e.detail.cloudID,
      encryptedData:e.detail.encryptedData,
      iv:e.detail.iv
    })
    const transmit_data = {
      "avatarurl": this.data.userInfo.avatarUrl,
      "city": this.data.userInfo.city,
      "ckName": this.data.userInfo.nickName,
      "country": this.data.userInfo.country,
      "encryptedData": this.data.encryptedData,
      "iv": this.data.iv,
      "jsCode": this.data.code,
      "openId": this.data.openId,
      "province": this.data.province     
    }
    requrestUrl.requestUrl({
        url: "/app/login/login",
        params: transmit_data
    }).then((data) => {
     let token = data.data.token;
     wx.setStorageSync('token', token)
     wx.navigateBack();
     // wx.switchTab({
     //   url: '../home/index',
     // })
    }).catch((errorMsg) => {
                
    })

  },
  getUserProfile (e) {

    const that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        that.setData({
          userInfo: res.userInfo
        })
        requrestUrl.requestUrl({
          url: "/app/login/login1",
          params: {
            "openId": that.data.openId
          }
        }).then((data) => {
          if (data.data.code == 102) {
            that.setData({
              showBut:2
            })
          }else{
            let token = data.data.token;
            wx.setStorageSync('token', token);
            wx.navigateBack();
          }
        }).catch((errorMsg) => {

        })
      }
    })
    

    

    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
          var resID = res.result.openid
          this.setData({
              openId:resID
          })
      },
      fail: err => {
          console.error('[云函数] [login] 调用失败', err)
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
        wx.login({
            success(res) {
                that.setData({
                    code: res.code
                })
            }
        })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})